/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.webaccess.retailws.Action;

import com.webaccess.retailws.Bean.BrandBean;
import com.webaccess.retailws.Bean.CategoryBean;
import com.webaccess.retailws.Delegate.BrandDelegate;
import com.webaccess.retailws.Delegate.CategoryDelegate;
import com.webaccess.retailws.Util.BrandUtil;
import com.webaccess.retailws.Util.CategoryUtil;
import com.webaccess.retailws.Util.CommonUtil;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.action.DynaActionForm;
import org.apache.struts.actions.DispatchAction;
import org.apache.struts.upload.FormFile;

/**
 *
 * @author Owner
 */
public class BrandDispatchAction extends DispatchAction {

    private String SUCCESS = "success";
    CategoryBean categBean = null;
    BrandUtil brandUtilObj = null;
    BrandBean brandBeanObj = null;
    ActionMessages errors = null;
    CategoryUtil categUtil = new CategoryUtil();
    CommonUtil commUtil = new CommonUtil();
    BrandDelegate brandDelegateObj = null;
    CategoryDelegate categDelegate = new CategoryDelegate();

    public ActionForward showAllBrands(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        brandDelegateObj = new BrandDelegate();

        try {
            commUtil.getTopRightListMap(request);
            request.setAttribute("brandListA", brandDelegateObj.searchBrandByName("A"));
            request.setAttribute("brandListB", brandDelegateObj.searchBrandByName("B"));
            request.setAttribute("brandListC", brandDelegateObj.searchBrandByName("C"));
            request.setAttribute("brandListD", brandDelegateObj.searchBrandByName("D"));
            request.setAttribute("brandListE", brandDelegateObj.searchBrandByName("E"));
            request.setAttribute("brandListF", brandDelegateObj.searchBrandByName("F"));
            request.setAttribute("brandListG", brandDelegateObj.searchBrandByName("G"));
            request.setAttribute("brandListH", brandDelegateObj.searchBrandByName("H"));
            request.setAttribute("brandListI", brandDelegateObj.searchBrandByName("I"));
            request.setAttribute("brandListJ", brandDelegateObj.searchBrandByName("J"));
            request.setAttribute("brandListK", brandDelegateObj.searchBrandByName("K"));
            request.setAttribute("brandListL", brandDelegateObj.searchBrandByName("L"));
            request.setAttribute("brandListM", brandDelegateObj.searchBrandByName("M"));
            request.setAttribute("brandListN", brandDelegateObj.searchBrandByName("N"));
            request.setAttribute("brandListO", brandDelegateObj.searchBrandByName("O"));
            request.setAttribute("brandListP", brandDelegateObj.searchBrandByName("P"));
            request.setAttribute("brandListQ", brandDelegateObj.searchBrandByName("Q"));
            request.setAttribute("brandListR", brandDelegateObj.searchBrandByName("R"));
            request.setAttribute("brandListS", brandDelegateObj.searchBrandByName("S"));
            request.setAttribute("brandListT", brandDelegateObj.searchBrandByName("T"));
            request.setAttribute("brandListU", brandDelegateObj.searchBrandByName("U"));
            request.setAttribute("brandListV", brandDelegateObj.searchBrandByName("V"));
            request.setAttribute("brandListW", brandDelegateObj.searchBrandByName("W"));
            request.setAttribute("brandListX", brandDelegateObj.searchBrandByName("X"));
            request.setAttribute("brandListY", brandDelegateObj.searchBrandByName("Y"));
            request.setAttribute("brandListZ", brandDelegateObj.searchBrandByName("Z"));

            SUCCESS = "allbrandsshow";

        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            return mapping.findForward(SUCCESS);
        }
    }

    public ActionForward showAddBrand(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        try {
            commUtil.getTopRightListMap(request);
            request.setAttribute("statusList", commUtil.getStatus());
            saveToken(request);
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            SUCCESS = "addbrandshow";
            return mapping.findForward(SUCCESS);
        }
    }

    public ActionForward showEditBrand(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        brandBeanObj = new BrandBean();
        brandDelegateObj = new BrandDelegate();
        Long brandId = 0L;
        try {
            commUtil.getTopRightListMap(request);
            request.setAttribute("statusList", commUtil.getStatus());
            brandId = Long.parseLong(request.getParameter("brandId"));

            brandBeanObj = brandDelegateObj.getBrand(brandId);
            request.setAttribute("brand", brandBeanObj);
            saveToken(request);
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            SUCCESS = "editbrandshow";
            return mapping.findForward(SUCCESS);
        }
    }

    public ActionForward showManageBrand(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        brandDelegateObj = new BrandDelegate();
        try {
            commUtil.getTopRightListMap(request);
            request.setAttribute("statusList", commUtil.getStatus());
            BrandBean[] brandBeanList = brandDelegateObj.getAllBrand();
            request.setAttribute("brandList", brandBeanList);
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            SUCCESS = "managebrandshow";
            return mapping.findForward(SUCCESS);
        }
    }

    public ActionForward searchBrandbyName(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        brandDelegateObj = new BrandDelegate();
        brandBeanObj = new BrandBean();
        String brandName = "";
        try {
            commUtil.getTopRightListMap(request);
            request.setAttribute("statusList", commUtil.getStatus());
            brandName = request.getParameter("searchBrand");
            if (brandName.equalsIgnoreCase("All")) {
                SUCCESS = "brandsuccess";
            } else {
                BrandBean[] brandBeanList = brandDelegateObj.searchBrandByName(brandName);
                request.setAttribute("brandList", brandBeanList);
                SUCCESS = "managebrandshow";
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            return mapping.findForward(SUCCESS);
        }
    }

    public ActionForward searchBrandbyStatus(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        brandDelegateObj = new BrandDelegate();
        brandBeanObj = new BrandBean();
        Long brandStatusId = 0L;
        try {
            commUtil.getTopRightListMap(request);
            request.setAttribute("statusList", commUtil.getStatus());
            brandStatusId = Long.parseLong(request.getParameter("brandStatusId"));
            if (brandStatusId == 0) {
                SUCCESS = "brandsuccess";
            } else {
                BrandBean[] brandBeanList = brandDelegateObj.searchBrandByStatus(brandStatusId);
                request.setAttribute("brandList", brandBeanList);
                SUCCESS = "managebrandshow";
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            return mapping.findForward(SUCCESS);
        }
    }

    public ActionForward deleteBrand(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        brandDelegateObj = new BrandDelegate();
        Long brandId = 0L;
        try {
            commUtil.getTopRightListMap(request);
            request.setAttribute("statusList", commUtil.getStatus());
            brandId = Long.parseLong(request.getParameter("brandId"));

            boolean isBrandDeleted = brandDelegateObj.deleteBrand(brandId);
            if (isBrandDeleted == true) {
                SUCCESS = "brandsuccess";
            } else {
                SUCCESS = "deletebrandfailure";
            }
        } catch (Exception ex) {
            SUCCESS = "deletebrandfailure";
            ex.printStackTrace();
        } finally {
            return mapping.findForward(SUCCESS);
        }
    }

    public ActionForward updateBrand(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        brandDelegateObj = new BrandDelegate();
        brandUtilObj = new BrandUtil();
        errors = new ActionMessages();

        String brandName = "", brandTitle = "", brandDesc = "", filePath = "", brandLogoName = "";
        Long brandStatusId = 0L, brandId = 0L;
        try {
            commUtil.getTopRightListMap(request);
            DynaActionForm brandDynaForm = (DynaActionForm) form;
            if (isTokenValid(request)) {
                brandId = Long.parseLong(request.getParameter("brandId"));
                brandName = String.valueOf(brandDynaForm.getString("brandName"));
                brandTitle = String.valueOf(brandDynaForm.getString("brandTitle"));
                brandDesc = String.valueOf(brandDynaForm.getString("brandDesc"));
                brandStatusId = Long.parseLong(request.getParameter("brandStatusId"));
                FormFile brandLogo = (FormFile) brandDynaForm.get("brandLogo");
                brandLogoName = String.valueOf(brandDynaForm.getString("brandLogoName"));
                Pattern fileType = Pattern.compile("/.*\\.(gif)|(jpeg)|(jpg)|(png)$/");
                Matcher matchFileType = fileType.matcher(brandLogo.getContentType());

                if (brandName.equalsIgnoreCase("") || brandTitle.equalsIgnoreCase("") || brandDesc.equalsIgnoreCase("") || brandStatusId == 0) {
                    errors.add("errBrand", new ActionMessage("errors.brand.required"));
                } else if (brandLogo.getFileSize() != 0) {
                    if (!matchFileType.find()) {
                        errors.add("errBrandLogo", new ActionMessage("errors.brandlogo.filetype"));
                    } else if (brandLogo.getFileSize() > 1048576) { //1MB
                        errors.add("errBrandLogo", new ActionMessage("errors.brandlogo.size"));
                    } else {
                        filePath = getServlet().getServletContext().getRealPath("/") + "brands";
                        brandLogoName = brandUtilObj.uploadFile(brandLogo, filePath);

                        SUCCESS = brandUtilObj.saveBrand(brandId, brandName, brandLogoName, brandDesc, brandTitle, brandStatusId);
                    }
                } else {

                    SUCCESS = brandUtilObj.saveBrand(brandId, brandName, brandLogoName, brandDesc, brandTitle, brandStatusId);
                }
                resetToken(request);
                saveErrors(request, errors);
                if (errors.isEmpty()) {
                    SUCCESS = "brandsuccess";
                } else {
                    SUCCESS = "updatebrandfailure";
                }
            }
            saveToken(request);
        } catch (Exception ex) {
            SUCCESS = "updatebrandfailure";
            ex.printStackTrace();
        } finally {
            return mapping.findForward(SUCCESS);
        }
    }

    public ActionForward saveBrand(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        brandDelegateObj = new BrandDelegate();
        brandUtilObj = new BrandUtil();
        errors = new ActionMessages();

        String brandName = "", brandTitle = "", brandDesc = "", filePath = "", brandLogoName = "";
        Long brandStatusId = 0L;
        try {
            commUtil.getTopRightListMap(request);
            DynaActionForm brandDynaForm = (DynaActionForm) form;
            if (isTokenValid(request)) {
                brandName = String.valueOf(brandDynaForm.getString("brandName"));
                brandTitle = String.valueOf(brandDynaForm.getString("brandTitle"));
                brandDesc = String.valueOf(brandDynaForm.getString("brandDesc"));
                brandStatusId = Long.parseLong(request.getParameter("brandStatusId"));
                FormFile brandLogo = (FormFile) brandDynaForm.get("brandLogo");

                Pattern fileType = Pattern.compile("/.*\\.(gif)|(jpeg)|(jpg)|(png)$/");
                Matcher matchFileType = fileType.matcher(brandLogo.getContentType());

                if (brandName.equalsIgnoreCase("") || brandTitle.equalsIgnoreCase("") || brandDesc.equalsIgnoreCase("") || brandStatusId == 0) {
                    errors.add("errBrand", new ActionMessage("errors.brand.required"));
                } else if (brandLogo.getFileSize() != 0) {
                    if (!matchFileType.find()) {
                        errors.add("errBrandLogo", new ActionMessage("errors.brandlogo.filetype"));
                    } else if (brandLogo.getFileSize() > 1048576) { //1MB
                        errors.add("errBrandLogo", new ActionMessage("errors.brandlogo.size"));
                    } else {
                        filePath = getServlet().getServletContext().getRealPath("/") + "brands";
                        brandLogoName = brandUtilObj.uploadFile(brandLogo, filePath);

                        SUCCESS = brandUtilObj.saveBrand(null, brandName, brandLogoName, brandDesc, brandTitle, brandStatusId);
                    }
                } else {
                    brandLogoName = "no_image.gif";
                    SUCCESS = brandUtilObj.saveBrand(null, brandName, brandLogoName, brandDesc, brandTitle, brandStatusId);
                }
                resetToken(request);
                saveErrors(request, errors);
                if (errors.isEmpty()) {
                    SUCCESS = "brandsuccess";
                } else {
                    SUCCESS = "savebrandfailure";
                }
            }
            saveToken(request);
        } catch (Exception ex) {
            SUCCESS = "savebrandfailure";
            ex.printStackTrace();
        } finally {
            return mapping.findForward(SUCCESS);
        }
    }
}
